package br.com.AC.modelo;

import java.sql.DriverManager;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

import br.com.AC.entidades.Consulta;
import br.com.AC.entidades.Imc;
import br.com.AC.entidades.Medico;
import br.com.AC.entidades.Usuario;

public class PacienteModel {
	
	private String Nome;
	private List<Usuario> tListaPaciente;

	Consulta con = new Consulta();
	Imc imc = new Imc();

	public String pesqUsu(int idUsu) {  
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("tcc");
		EntityManager em = emf.createEntityManager();
		 try {                 
			 Query query = em.createQuery("FROM Usuario WHERE id_usuario = :idUsu");    
	         query.setParameter("idUsu", idUsu);              
	         Usuario result = (Usuario) query.getSingleResult();
	            
	            	
	         return result.getNome();
	        }   
	        catch (Exception e) {  
	            System.out.println("Erro:   "+e);  
	            em.close();
	            return "fail";  
	        }   
	        finally {  
	        	em.close();
	        }  
	    }
	
	public List<Usuario> listaPaciente(int id) {  

		EntityManagerFactory emf = Persistence.createEntityManagerFactory("tcc");
		EntityManager em = emf.createEntityManager();
		 try {                 
			 Date data = new Date(System.currentTimeMillis());
 			 SimpleDateFormat formDate = new SimpleDateFormat("yyyy-MM-dd");

			 Query query1 = em.createQuery("FROM Medico WHERE Usuario_id_usuario = :id");    
	         query1.setParameter("id", id);              
	         Medico result1 = (Medico) query1.getSingleResult();

			 Query query2 = em.createQuery("FROM Consulta WHERE Medico_crm = :crm " +
			 		" AND dt_consult = '"+formDate.format(data)+"'" +
			 		" AND Paciente_Usuario_id_usuario <> 9999");    
	         query2.setParameter("crm", result1.getCrm());              
	         List<Consulta> result2 = (List<Consulta>) query2.getResultList();
	         
	 		tListaPaciente = new ArrayList<Usuario>();

			 for (int i =0; i<result2.size();i++){
		         Query query3 = em.createQuery("FROM Usuario WHERE id_usuario = :idUsu");    
		         query3.setParameter("idUsu", result2.get(i).getPaciente_Usuario_id_usuario());              
		         Usuario result3 = (Usuario) (query3.getSingleResult());
		         tListaPaciente.add(result3);
		     }
			 return tListaPaciente;
			 
	        }   
	        catch (Exception e) {  
	        	 System.out.println("Erro:   "+e);  
				 return tListaPaciente;
	        }   
	        finally {  
	        }  
	    }
	

	
	public String incluirConsulta(Object consulta){
		try{

			EntityManagerFactory emf = Persistence.createEntityManagerFactory("tcc");

            EntityManager em = emf.createEntityManager();

            EntityTransaction tx = em.getTransaction();
			tx.begin();

			con = (Consulta) consulta;
			
			em.merge(con);

			tx.commit();
			
			em.close();
	}
	catch (Exception e) {
		return "fail";
	}	

	return"ok";
	}
	

	public List<Imc> pesqImc(int idPaciente){
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("tcc");
		EntityManager em = emf.createEntityManager();
		try {                 
			 Query query = em.createQuery("FROM Imc WHERE paciente_Usuario_id_usuario = "+idPaciente+
			 							  " ORDER BY dt_atul ASC");    
	         
	         List<Imc> result = (List<Imc>) query.getResultList();
	            
	         return result;
	            	
	        }   
	        catch (Exception e) {  
	            System.out.println("Erro:   "+e);  
	            return null;  
	        }   
	        finally {  
	        }  
	}
	

	public String atualizaImc(Object imcp) 
	{
		
		try{
	
			EntityManagerFactory emf = Persistence.createEntityManagerFactory("tcc");
	
	        EntityManager em = emf.createEntityManager();
	
	        EntityTransaction tx = em.getTransaction();
			tx.begin();
	
			imc = (Imc) imcp;
	
			em.persist(imc);
			
			tx.commit();
			
			em.close();
		}
		catch (Exception e) {
			return "fail";
		}	
	
	return"ok";
	}
	
	public List<Consulta> listaConsMarcadas(int idPaciente){
		
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("tcc");
		EntityManager em = emf.createEntityManager();
		try {            
			
			SimpleDateFormat formDate = new SimpleDateFormat("yyyyMMdd");
			Date date = new Date();
			String dataAtual = formDate.format(date);
			 Query query = em.createQuery("FROM Consulta WHERE paciente_Usuario_id_usuario = "+idPaciente+
					" and dt_consult >= "+ dataAtual);    
	         
	         List<Consulta> result =  query.getResultList();
	            
	         return result;
	            	
	        }   
	        catch (Exception e) {  
	            System.out.println("Erro:   "+e);  
	            return null;  
	        }   
	        finally {  
	        } 
	
	}
	
	public List<Consulta> listaConsultas(int idPaciente){
		
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("tcc");
		EntityManager em = emf.createEntityManager();
		try {            
			
			 Query query = em.createQuery("FROM Consulta WHERE paciente_Usuario_id_usuario = "+idPaciente);
	         
	         List<Consulta> result =  query.getResultList();
	            
	         return result;
	            	
	        }   
	        catch (Exception e) {  
	            System.out.println("Erro:   "+e);  
	            return null;  
	        }   
	        finally {  
	        } 
	
	}
	

	
	public String getNome() {
		return Nome;
	}

	public void setNome(String nome) {
		Nome = nome;
	}
	

}
